package com.csdj.youxi.controller;

import com.alibaba.fastjson.JSON;
import com.csdj.youxi.entity.DevUser;
import com.csdj.youxi.service.DevUserService;
import com.csdj.youxi.service.IDevUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class DevUserController extends AbstractController {

    @Override
    protected ModelAndView handleRequestInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        return null;
    }

    @Resource
    private IDevUserService devUserService;

    @RequestMapping("/tologin")
    public String tologin(){
        return "Login";
    }


    @ResponseBody
    @PostMapping("/login")
    public String login(@RequestParam(value = "devCode",required = false) String devCode,
                        @RequestParam(value = "devPassword",required = false) String devPassword,
                        HttpSession session){
        DevUser devUser = devUserService.login(devCode,devPassword);
        if(devUser!=null){
            session.setAttribute("devCode",devCode);
            session.setAttribute("devPassword",devPassword);
            session.setAttribute("userType",devUser.getUserType());
            return JSON.toJSONString(devUser.getUserType());
        }
        return null;
    }
}
